﻿using System;
using System.Data;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;

public class CaptchaImage
{
    public static Image GenericImage(out string _text, int _width, int _height)
    {
        Bitmap bmp = new Bitmap(_width, _height);
        Graphics g = Graphics.FromImage(bmp);
        Random rnd = new Random();

        // Fill in the background.        
        Rectangle rect = new Rectangle(0, 0, _width, _height);
        HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.Violet, Color.WhiteSmoke);
        g.FillRectangle(hatchBrush, rect);
        // Draw Line
        //g.DrawLine(new Pen(Color.Black, 1), new Point(rnd.Next(0, 30), _height / 2), new Point(_width, (_height / 2) + rnd.Next(3, 30)));
        g.DrawLine(new Pen(Color.Black, 1), new Point(0, _height / rnd.Next(2, 4)), new Point(_width, _height / rnd.Next(2, 6)));

        // Rotate
        //Matrix mt = new Matrix();
        ////mt.Shear(1, 0);
        //mt.Rotate(5);
        //g.Transform = mt;

        // Get random text
        string text = "qertyuiopasdfghjklzxcvbnm";
        string s = "";
        for (int i = 0; i < 5; i++)
        {
            s += text[rnd.Next(0, text.Length - 1)];
        }
        _text = s;

        // Draw text on the Image
        //Font f = new Font("Verdana", 20, FontStyle.Bold);
       // Font f = new Font("Curlz MT", 20, FontStyle.Bold);
        //Font f = new Font("Bradley Hand ITC", 20, FontStyle.Bold);
        Font f = new Font("Comic Sans MS", 25, FontStyle.Regular);
        Point p = new Point(30, 15);
        g.DrawString(s, f, Brushes.Black, p);

        // Draw Line
        //g.DrawLine(new Pen(Color.Black, 1), new Point(rnd.Next(0, 30), _height / 2), new Point(_width, (_height / 2) + rnd.Next(3, 30)));
        g.DrawLine(new Pen(Color.White, 1), new Point(0, _height / rnd.Next(2, 4)), new Point(_width, _height / rnd.Next(2, 6)));

        return bmp;
    }
}
